package unittests;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import static org.junit.Assert.*;
import org.junit.Test;
import spreadsheet.SpreadSheet;
import spreadsheet.SpreadSheetCell;

public class XMLWriterDOMTest {

	SpreadSheet spreadSheet = new SpreadSheet();
    
	@Test
	//This test will test method getCell by calling "saveSpreadSheet" from the SpreadSheet class.
	//This works because the "saveSpreadSheet" method calls on getCell. If results return
	//	as expected for "saveSpreadSheet", then this implies getCell() also works.
	//	In addition, by testing the getCell() method, getCellElement() is also tested.
	public void test() {
		spreadSheet = new SpreadSheet();
		spreadSheet.getCells()[0][0].setCellValue("=10 + 5");
		spreadSheet.getCells()[0][0].setCellType(SpreadSheetCell.CELL_TYPES_FORMULA);
		spreadSheet.getCells()[0][1].setCellValue("=A1");
		spreadSheet.getCells()[0][1].setCellType(SpreadSheetCell.CELL_TYPES_FORMULA);
		spreadSheet.getCells()[0][2].setCellValue("2");
		spreadSheet.getCells()[0][2].setCellType(SpreadSheetCell.CELL_TYPES_PRIMITIVE);
                spreadSheet.getCells()[0][3].setCellValue("");
		spreadSheet.getCells()[0][3].setCellType(SpreadSheetCell.CELL_TYPES_PRIMITIVE);
                spreadSheet.getCells()[0][4].setCellValue("");
		spreadSheet.getCells()[0][4].setCellType(SpreadSheetCell.CELL_TYPES_FORMULA);
                spreadSheet.getCells()[0][7].setCellValue(null);
		spreadSheet.getCells()[0][7].setCellType(SpreadSheetCell.CELL_TYPES_PRIMITIVE);
                spreadSheet.getCells()[0][8].setCellValue(null);
		spreadSheet.getCells()[0][8].setCellType(SpreadSheetCell.CELL_TYPES_FORMULA);
                spreadSheet.getCells()[0][5].setCellValue("10");
		spreadSheet.getCells()[0][5].setCellType(SpreadSheetCell.CELL_TYPES_FORMULA);
		spreadSheet.getCells()[0][6].setCellValue("=A1");
		spreadSheet.getCells()[0][6].setCellType(SpreadSheetCell.CELL_TYPES_PRIMITIVE);
		try {
			//Returns false if null is given for the path
			assertFalse(spreadSheet.saveSpreadSheet(null)); //Test 1
	
			//Returns true if file has been successfully saved.			
			assertTrue(spreadSheet.saveSpreadSheet("Test.xml")); //Test 3
                        //Verify that there is an XML file called "Test.xml" in the directory
			//of this project.
			assertTrue( new File( "Test.xml" ).exists() );
//                        check if file is empty
                        assertTrue( new File( "Test.xml" ).length() > 0 );
                        new File( "Test.xml" ).delete();
//                        for tests regarding the accuracy of the save please refer to the tests for reading a file
		} catch (FileNotFoundException e) {
			fail(e.getMessage());
		} catch (IOException e) {
			fail(e.getMessage());
		}
                try {
                    spreadSheet.saveSpreadSheet("/this/path/does/not/exist");
                    fail( "There should have been an exception thrown here" );
                } catch (FileNotFoundException e) {}  catch (IOException e) {}
	}
}